<!DOCTYPE HTML>
<html>
<head>
  <title>Test for audio controller in windows</title>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<iframe src="about:blank" id="iframe"></iframe>
<script type="application/javascript">

function runTest() {
  var utils = SpecialPowers.wrap(window).
        QueryInterface(SpecialPowers.Ci.nsIInterfaceRequestor).
        getInterface(SpecialPowers.Ci.nsIDOMWindowUtils);
  ok(utils, "nsIDOMWindowUtils");

  is(utils.audioMuted, false, "By default utils.audioMuted is false");
  utils.audioMuted = true;
  is(utils.audioMuted, true, "utils.audioMuted is true");
  utils.audioMuted = false;
  is(utils.audioMuted, false, "utils.audioMuted is true");

  is(utils.audioVolume, 1.0, "By default utils.audioVolume is 1.0");
  utils.audioVolume = 0.4;
  is(utils.audioVolume.toFixed(2), "0.40", "utils.audioVolume is ok");
  utils.audioMuted = true;
  is(utils.audioMuted, true, "utils.audioMuted is true");
  is(utils.audioVolume.toFixed(2), "0.40", "utils.audioVolume is ok");
  utils.audioMuted = false;

  utils.audioVolume = 2.0;
  is(utils.audioVolume, 2.0, "utils.audioVolume is ok");

  try {
    utils.audioVolume = -42;
    ok(false, "This should throw");
  } catch(e) {
    ok(true, "This should throw");
  }

  utils.audioVolume = 0;
  is(utils.audioVolume, 0.0, "utils.audioVolume is ok");
  utils.audioVolume = 1.0;
  is(utils.audioVolume, 1.0, "utils.audioVolume is ok");

  var iframe = document.getElementById("iframe");
  ok(iframe, "IFrame exists");

  utils = SpecialPowers.wrap(iframe.contentWindow).
        QueryInterface(SpecialPowers.Ci.nsIInterfaceRequestor).
        getInterface(SpecialPowers.Ci.nsIDOMWindowUtils);
  ok(utils, "nsIDOMWindowUtils");

  is(utils.audioMuted, false, "By default utils.audioMuted is false");
  utils.audioMuted = true;
  is(utils.audioMuted, true, "utils.audioMuted is true");
  utils.audioMuted = false;
  is(utils.audioMuted, false, "utils.audioMuted is true");

  is(utils.audioVolume, 1.0, "By default utils.audioVolume is 1.0");
  utils.audioVolume = 0.4;
  is(utils.audioVolume.toFixed(2), "0.40", "utils.audioVolume is ok");
  utils.audioMuted = true;
  is(utils.audioMuted, true, "utils.audioMuted is true");
  is(utils.audioVolume.toFixed(2), "0.40", "utils.audioVolume is ok");
  utils.audioMuted = false;

  utils.audioVolume = 2.0;
  is(utils.audioVolume, 2.0, "utils.audioVolume is ok");

  try {
    utils.audioVolume = -42;
    ok(false, "This should throw");
  } catch(e) {
    ok(true, "This should throw");
  }

  utils.audioVolume = 0;
  is(utils.audioVolume, 0.0, "utils.audioVolume is ok");
  utils.audioVolume = 0.6;
  is(utils.audioVolume.toFixed(2), "0.60", "utils.audioVolume is ok");
  utils.audioMuted = true;

  // Navigate the iframe to another URL, and verify that the volume and muted
  // information is preserved.
  iframe.onload = function() {
    utils = SpecialPowers.wrap(iframe.contentWindow).
          QueryInterface(SpecialPowers.Ci.nsIInterfaceRequestor).
          getInterface(SpecialPowers.Ci.nsIDOMWindowUtils);
    ok(utils, "nsIDOMWindowUtils");

    ok(utils.audioMuted, "Audio should still be muted");
    utils.audioMuted = false;
    ok(utils.audioVolume.toFixed(2), "0.60", "Volume should be preserved");

    SimpleTest.finish();
  };
  iframe.src = "data:text/html,page";
}

onload = runTest;
SimpleTest.waitForExplicitFinish();

</script>
</body>
</html>
